#include "configuration.h"

#include <sys/stat.h>

#include <fstream>
using std::ofstream;
using std::ifstream;
using std::endl;
#include <sstream>
using std::istringstream;
#include <string>
using std::getline;
using std::string;

/* init default values in constructor */
configuration::configuration (const ustring &nconfig_file_name):
config_file_path_ (nconfig_file_name),
soft_interval(5),  /* s */
hard_interval(20), /* s */
memory_valve(200), /* mb */
cpu_valve(50), /* % */
io_valve(5), /* mb/s */
enable_notification(true),
disable_io_notification(true)
{
    /* read configuration file */
    ifstream file ((config_file_path_ + "/config").c_str ());
    if (!file) return;

    string line;
    while (getline (file, line)){
        istringstream ss (line);
        string value_name;
        ss >> value_name;

        if (value_name == "soft_interval")
            ss >> soft_interval;
        else if (value_name == "hard_interval")
            ss >> hard_interval;
        else if (value_name == "memory_valve")
            ss >> memory_valve;
        else if (value_name == "cpu_valve")
            ss >> cpu_valve;
        else if (value_name == "io_valve")
            ss >> io_valve;
        else if (value_name == "enable_notification")
            ss >> enable_notification;
        else if (value_name == "disable_io_notification")
            ss >> disable_io_notification;
    }
}

bool configuration::save () const {
    mkdir (config_file_path_.c_str (), 0755);

    ofstream file ((config_file_path_ + "/config").c_str ());

    file << "soft_interval " << soft_interval << endl
         << "hard_interval " << hard_interval << endl
         << "memory_valve " << memory_valve << endl
         << "cpu_valve " << cpu_valve << endl
         << "io_valve " << io_valve << endl
         << "enable_notification " << enable_notification << endl
         << "disable_io_notification " << disable_io_notification << endl;

    file.close ();

    /* signal_quit requires returning a bool */
    return false;
}
